//
// Created by wjxfhxy on 15-1-9.
// Copyright (c) 2015 GuangZhouXuanWu. All rights reserved.
//

#import "ImageBrowserViewController.h"

#import "AssetHelper.h"

@interface ImageBrowserViewController () <XUIButtonDelegate>
{
    XUIButton *_saveImageBtn;

    XMSQueueDictionary *_qdDownloaded;
}

@end

@implementation ImageBrowserViewController

- (id)initWithImagePaths:(NSArray *)paths
{
    _qdDownloaded = [XMSQueueDictionary new];
    for (NSString *path in paths)
        [_qdDownloaded addKey:[path lastPathComponent] value:@(YES)];

    return [super initWithImagePaths:paths];
}

- (id)initWithImageURL:(NSArray *)originImageURLs downloadSavePath:(NSString *)savePath
{
    _qdDownloaded = [XMSQueueDictionary new];
    for (NSString *path in originImageURLs)
        [_qdDownloaded addKey:[path lastPathComponent] value:@(NO)];

    return [super initWithImageURL:originImageURLs downloadSavePath:savePath];
}

- (id)initWithThumbImagePath:(NSArray *)thumbImagePaths originImageURL:(NSArray *)originImageURLs downloadSavePath:(NSString *)savePath
{
    _qdDownloaded = [XMSQueueDictionary new];
    for (NSString *path in originImageURLs)
        [_qdDownloaded addKey:[path lastPathComponent] value:@(NO)];

    return [super initWithThumbImagePath:thumbImagePaths originImageURL:originImageURLs downloadSavePath:savePath];
}

- (id)initWithImages:(NSArray *)images
{
    _qdDownloaded = [XMSQueueDictionary new];
    for (UIImage *image in images)
    {
        (void)image;
        [_qdDownloaded addKey:@"" value:@(YES)];
    }
    return [super initWithImages:images];
}

- (void)initViewDidLoad
{
    [super initViewDidLoad];

    _saveImageBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCenterImage delegate:self];
    _saveImageBtn.frame = CGRectMake(self.view.width - 10 - 40, self.view.height - 10 - 40, 40, 40);
    [_saveImageBtn SetImage:[UIImage imageNamed:@"btn_save_image.png"]];
    _saveImageBtn.hidden = ![[_qdDownloaded getValueForIndex:[self currentIndex]] boolValue];
    [self.view addSubview:_saveImageBtn];
}

- (void)clickedUpInsideOfButton:(XUIButton *)btn
{
    if ([[_qdDownloaded getValueForIndex:[self currentIndex]] boolValue] != YES)
    {
        return;
    }

    UIImage *image = [super currentOriginalImage];

    if (image == nil)
        return;

    [self beginProgressViewPanel:@"图片保存中，请稍候"];

    [[AssetHelper sharedAssetHelper] writeImageToSavedPhotosAlbum:image completionBlock:^(NSURL *assetURL, NSError *error)
    {
        [self endProgressViewPanel];

        if (error == nil)
        {
            [UIAlertView showInfoMsg:@"图片已保存至相册"];
        }
        else
        {
            [UIAlertView showErrorMsg:@"图片保存至相册失败"];
        }
    }];
}

- (void)previewImageViewControllerFinishDownloadImage:(NSString *)imageFilePath atIndex:(NSUInteger)index
{
    [_qdDownloaded setValue:@(YES) atIndex:index];

    if ([[_qdDownloaded getKeyForIndex:[self currentIndex]] isEqualToString:[imageFilePath lastPathComponent]])
    {
        _saveImageBtn.hidden = NO;
    }
}

- (void)didShowImageAtIndex:(NSUInteger)index andIsOriginalImage:(BOOL)original
{
    if (original == YES)
    {
        [_qdDownloaded setValue:@(YES) atIndex:index];

        _saveImageBtn.hidden = NO;
    }
    else
    {
        [_qdDownloaded setValue:@(NO) atIndex:index];

        _saveImageBtn.hidden = YES;
    }
}

//- (void)didScrollToNewImageAtIndex:(NSUInteger)newIndex
//{
//    if([[_qdDownloaded getValueForIndex:newIndex] boolValue] == YES)
//    {
//        _saveImageBtn.hidden = NO;
//    }
//    else
//    {
//        _saveImageBtn.hidden = YES;
//    }
//}

- (void)previewImageViewControllerWillAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
    {
        _saveImageBtn.frame = CGRectMake(self.view.height - 10 - 40, self.view.width - 10 - 40, 40, 40);
    }
    else
    {
        _saveImageBtn.frame = CGRectMake(self.view.width - 10 - 40, self.view.height - 10 - 40, 40, 40);
    }
    
}

@end